package vn.vfriends.vbox.bean;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.vbox.model.Role;
import vn.vfriends.vbox.service.RoleService;

/**
 *
 * @author tuancn
 */
@Named
@ViewScoped
public class RoleBean implements Serializable {
    
    private static final Logger logger = LoggerFactory.getLogger(RoleBean.class);
    
    @Inject
    private RoleService roleService;
    
    private Role[] selectedRoles;
    private Role role;
    
    private Integer roleId;

    public Role[] getSelectedRoles() {
        return selectedRoles;
    }

    public void setSelectedRoles(Role[] selectedRoles) {
        this.selectedRoles = selectedRoles;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }
    
    public List<Role> getRoleList() {
        return this.roleService.findAll();
    }
    
    public void preNewAction() {
        RequestContext.getCurrentInstance().openDialog("new");
    }
    
    public void preRenderNewView() {
        this.role = new Role();
    }
    
    public void saveAction() {
        try {
            this.roleService.create(this.role);
            
            RequestContext.getCurrentInstance().closeDialog("newDialog");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Info", "Enternal Server Error");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void preEditAction() {
        try {
            if (this.selectedRoles == null || this.selectedRoles.length == 0) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Please select role");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            } else if (this.selectedRoles.length > 1) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Please select only a role");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            } else {
                this.role = this.selectedRoles[0];
                Map<String,List<String>> params = new HashMap<>();
                params.put("roleId", Arrays.asList(this.role.getId().toString()));
                RequestContext.getCurrentInstance().openDialog("edit", null, params);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Info", "Internal Server Error");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void preRenderEditView() {
        this.role = this.roleService.find(roleId);
        if (this.role == null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Info", "The role is not exist");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
    
    public void updateAction() {
        try {
            this.roleService.edit(this.role);
            RequestContext.getCurrentInstance().closeDialog("editDialog");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Info", "Intenal Server Error");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void preDeleteAction() {
        try {
            if (this.selectedRoles == null || this.selectedRoles.length == 0) {
                RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Please select role(s)");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Info", "Intenal Server Error");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void deleteAction() {
        try {
            for(Role selectedRole : selectedRoles) {
                this.roleService.remove(selectedRole);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Info", "Intenal Server Error");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
}
